﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Collections.Generic;
using System.Web;

namespace achappey.SharePoint.Livestamp
{
    public partial class LivestampFieldEditor : UserControl, IFieldEditor
    {
        #region WebControls

        protected DropDownList ddlFields;

        #endregion

        #region IFieldEditor Members

        public bool DisplayAsNewSection
        {
            get 
            { 
                return false; 
            }
        }

        public void InitializeWithField(SPField field)
        {
            // If it's no postback (first time loading on Edit Field)
            if (!IsPostBack && HttpContext.Current.Request.QueryString["List"] != null)
            {
                LivestampField myField = field as LivestampField;
                List<ListItem> items = new List<ListItem>();

                SPList list = SPContext.Current.Web.Lists[new Guid(Server.UrlDecode(HttpContext.Current.Request.QueryString["List"].ToString()))];

                SPFieldCollection fields = list.Fields;

                SPSite site = SPContext.Current.Site;

                foreach (SPField otherField in fields)
                {
                    if (otherField.Type == SPFieldType.DateTime
                        || (otherField.Type == SPFieldType.Calculated && (otherField as SPFieldCalculated).OutputType == SPFieldType.DateTime))
                        items.Add(new ListItem(otherField.Title, otherField.InternalName));
                    else
                    {
                        if (otherField.Type == SPFieldType.Lookup
                            && !string.IsNullOrEmpty((otherField as SPFieldLookup).LookupList)
                            && !(otherField as SPFieldLookup).IsDependentLookup)
                        {
                            SPFieldLookup fieldLookup = otherField as SPFieldLookup;

                            using (SPWeb web = site.OpenWeb(fieldLookup.LookupWebId))
                            {
                                SPList lookupList = web.Lists[new Guid(fieldLookup.LookupList)];

                                if (lookupList.Fields[fieldLookup.LookupField].Type == SPFieldType.DateTime)
                                    items.Add(new ListItem(otherField.Title, otherField.InternalName));
                            }
                        }
                    }
                }

                items.Sort((a, b) => a.Text.CompareTo(b.Text));

                ddlFields.DataSource = items;
                ddlFields.DataBind();

                if (myField != null && ddlFields.Items.FindByValue(myField.FieldName) != null)
                    ddlFields.SelectedValue = myField.FieldName;
            }
        }

        public void OnSaveChange(SPField field, bool isNewField)
        {
            LivestampField myField = field as LivestampField;

            if (null != myField) 
                myField.SetField(ddlFields.SelectedValue);
        }

        #endregion
    }
}
